<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-22
 * Time: 上午9:22
 */
class Article extends MY_Controller
{
    protected $article = array(
        array(
            'field' => 'title',
            'label' => '标题',
            'rules' => 'trim|required|xss_clean|min_length[1]|max_length[200]'
        ),
        array(
            'field' => 'content',
            'label' => '内容',
            'rules' => 'trim|required|xss_clean'
        ),
        array(
            'field' => 'is_zan',
            'label' => '是否赞赏',
            'rules' => 'trim|required|xss_clean'
        )
    );

    function __construct()
    {
        parent::__construct();
        $this->_auto();
        $this->load->model('long_article_model','mlong_article');
    }

    public function index()
    {
        $this->vdata['header'] = array(
            'title' => $this->mcfg->get_config('site', 'title_seo'),
            'tags' => $this->mcfg->get_config('site', 'tags'),
            'intro' => $this->mcfg->get_config('site', 'intro')
        );
        # 在使用控制器时输出CI共模板使用
        $this->vdata['CI'] =& $this;
        if($this->userinfo['ctype'] != 1 || !$this->userinfo['is_company']) {
            $is_power = false;
        } else {
            $is_power = true;
        }
        $this->vdata['is_power'] = $is_power;
        $this->load->view('article', $this->vdata);
    }
    public function add() {
        $this->form_validation->set_rules($this->article);
        if ($this->form_validation->run() == false) {
            $this->vdata['status'] = 0;
            $this->vdata['msg'] = form_errors();
        } else {
            $data = array_merge($this->input->post(),array('uid'=>$this->uid));
            $id = $this->mlong_article->create($data);
            if($id) {
                $this->load->model('circle_model','mcircle');
                $create_data = array(
                    'uid'=>$this->uid,
                    't_id'=>$id,    //长文章ID
                    'c_type'=>2,
                    'title'=>$data['title'],
                    'content'=>$data['content'],
                    'timeline'=>time()
                );
                $this->mcircle->create($create_data);
            }
            $this->vdata['status'] = 1;
            $this->vdata['msg'] = '发布成功';
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
    }
}